package h0904;

import java.util.Arrays;
import java.util.HashMap;
/**
  * @description 两数之和
  * @author 不知名帅哥
  * @date 2024/9/4 22:24
  * @version 1.0
*/
public class TwoSum {
    public static void main(String[] args) {
        int[] nums = new int[]{3, 2, 4};
        int[] res = twoSum(nums, 6);
        System.out.println(Arrays.toString(res));
    }

    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return new int[]{0, 0};
    }
}
